<?php

namespace App\Http\Controllers\Api;

use App\Http\Models\Medical;
use App\Http\Models\MemberDoctor;
use App\Services\EmployeeService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator as FacadesValidator;


/**
 * 我的
 *
 * @Author hechengwei
 * @email chichiooo@sina.com
 * @DateTime 2022-12-21
 */
class MemberController extends BaseController
{

    public function getMedicalLists()
    {
        $where['user_id'] = memberId();
        $model = new Medical();
        $list = $model->getListByUser($where, ['*']);
        return success($list);
    }


    /**
     * 我的病例
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-12-22
     * @param Request $request
     * @return json
     */
    public function getMedicalList(Request $request)
    {
        $rules = [
            'page' => ['required'],
            'limit' => ['required'],
            'status' => ['required'],
        ];

        $message = [
            'page.required' => '页码不能为空',
            'limit.required' => '条数不能为空',
            'status.required' => '状态不能为空',
        ];

        $payload = $request->only('page', 'limit', 'status');

        $validator = FacadesValidator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        $where['user_id'] = memberId();
        $where['status'] = $payload['status'];
        $model = new Medical();
        $list = $model->getMedicalListByUser((int) $payload['page'], (int) $payload['limit'], $where, ['id', 'name', 'sex', 'sex as sex_text', 'age', 'source', 'source as source_text', 'source_input', 'created_at']); //,'act_id', 'title', 'author', 'medical_history'
        return success($list);
    }

    /**
     * 查看审核意见
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-12-22
     * @param Request $request
     * @return void
     */
    public function getReason(Request $request)
    {
        $rules = [
            'id' => ['required'],
        ];

        $message = [
            'id.required' => 'id不能为空',
        ];

        $payload = $request->only('id');

        $validator = FacadesValidator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        $where['id'] = $payload['id'];

        $where['user_id'] = memberId();

        $res = Medical::getOne($where, ['id', 'review_comments']);

        if ($res) {
            return success($res);
        } else {
            return fail_message('没有权限');
        }
    }

    /**
     * 医师资格认证
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-03-17
     * @param Request $request
     * @return json
     */
    public function doctorCert(Request $request)
    {
        $rules = [
            'real_name' => ['required'],
            'unit' => ['required'],
            'department' => ['required'],
            'title' => ['required'],
            'phone' => ['required'],
            'id_card_number' => ['required'],
            'bank_number' => ['required'],
            'opening_bank' => ['required'],
            'certificate' => ['required', 'notimage'],
        ];

        $message = [
            'real_name.required' => '请输入真实姓名',
            'unit.required' => '请输入工作单位',
            'department.required' => '请输入科室',
            'title.required' => '请输入职称',
            'phone.required' => '请输入手机号码',
            'id_card_number.required' => '请输入身份证号码',
            'bank_number.required' => '请输入银行卡号',
            'opening_bank.required' => '请输入开户行',
            'certificate.required' => '请上传工牌照片',
            'certificate.notimage' => '工牌图片格式不正确',
        ];

        $payload = $request->only('real_name', 'unit', 'department', 'title', 'phone', 'id_card_number',  'bank_number', 'opening_bank', 'certificate');

        Log::notice('请求参数 : ' . json_encode($payload));

        $validator = FacadesValidator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        return success(MemberDoctor::doctorCert($payload, member()));
    }

    /**
     * 我的认证信息
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-12-22
     * @param Request $request
     * @return void
     */
    public function myCert(Request $request)
    {
        $info = MemberDoctor::where('user_id', memberId())->select('*', 'status as status_text')->first();
        if (!empty($info)) {
            return success($info);
        } else {
            return success([]);
        }
    }
}
